package com.jymj.common.aop;

import com.alibaba.fastjson.JSON;
import com.jymj.common.annotation.EnviorSupvs;
import com.jymj.dao.EnviorSupvsLogDao;
import com.jymj.entity.EnviorSupvsLogEntity;
import com.jymj.entity.SysUserEntity;
import com.jymj.entity.dto.PointPageDto;
import com.jymj.entity.landmanage.PointRecordEntity;
import com.jymj.service.EnviorSupvsService;
import com.jymj.utils.RRException;
import com.jymj.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author Lzh
 * @date 2021/3/16 15:20
 */
@Slf4j
@Aspect
@Component
@Transactional(rollbackFor = RuntimeException.class)
public class EnviorSupvsLog {
    @Autowired
    private EnviorSupvsService enviorSupvsService;
    @Autowired
    private EnviorSupvsLogDao enviorSupvsLogDao;

    @Around("@annotation(enviorSupvs)")
    public Object around(ProceedingJoinPoint joinPoint, EnviorSupvs enviorSupvs) {
        //获取注解内容
        String operation = enviorSupvs.operation();
        //获取参数
        Object[] args = joinPoint.getArgs();
        Object result = null;
        try {
            if (operation.equals("INSERT")) {
                operation = "新增问题点位";
                log.info("执行添加问题的日志");

                //执行目标方法
                result = joinPoint.proceed();
                String msg = saveEnviorSupvsLog((Long) result, operation, 1);
                log.info("{}:{}", msg, new Date());
            }
            if (operation.equals("DELETE")) {
                operation = "删除问题点位";
                Long arg = Long.valueOf(args[0].toString());
                log.info("执行删除问题的日志");

                //执行目标方法
                String msg = saveEnviorSupvsLog(arg, operation, 1);
                log.info("{}:{}", msg, new Date());
                result = joinPoint.proceed();
            }
            if (operation.equals("DELETEBATCH")) {
                operation = "批量删除问题点位";
                log.info("批量删除问题点位");
                Long[] arg = (Long[]) args[0];
                for (Long aLong : arg) {
                    String msg = saveEnviorSupvsLog(aLong, operation, 1);
                    log.info("{}:{}", msg, new Date());
                }
                //执行目标方法
                log.info("{}:{}", "执行目标方法，批量删除问题点位", new Date());
                result = joinPoint.proceed();
            }
            if (operation.equals("SAVE_POINT")) {
                operation = "记录固定点位";
                log.info("记录固定点位");
                //执行目标方法
                log.info("{}:{}", "执行目标方法，记录固定点位", new Date());
                PointRecordEntity pointRecordEntity = (PointRecordEntity) joinPoint.proceed();
                //记录日志
                String msg= saveEnviorSupvsLog(Long.valueOf(pointRecordEntity.getId()), operation, 2);
                log.info("{}:{}", msg, new Date());

            }
            if (operation.equals("UPDATE_POINT")){
                operation = "更新固定点为信息";
                log.info("更新固定点为信息");
                //执行目标方法
                log.info("{}:{}", "执行目标方法，更新固定点为信息", new Date());
                joinPoint.proceed();
                PointRecordEntity arg = (PointRecordEntity)args[0];
                //记录日志
                String msg = saveEnviorSupvsLog(Long.valueOf(arg.getId()), operation, 2);
                log.info("{}:{}", msg, new Date());
            }
            if (operation.equals("DELETE_POINT")){
                operation = "删除固定单位信息";
                log.info("删除固定单位信息");
                //记录被删除的数据
                Integer[] arg =(Integer[])args[0];
                for (Integer ids : arg) {
                    String msg = saveEnviorSupvsLog(ids.longValue(), operation, 2);
                    log.info("{}:{}", msg, new Date());
                }
                log.info("{}:{}", "执行目标方法，删除固定单位信息", new Date());
                joinPoint.proceed();
            }

            return result;
        } catch (Throwable throwable) {
            log.error("目标方法执行异常", throwable.getMessage());
            throw new RRException(throwable.getMessage());
        }

    }

    private String saveEnviorSupvsLog(Long enid, String operation, Integer type) {
        EnviorSupvsLogEntity enviorSupvsLogEntity = initEnviorSupvsLog(enid, operation, type);
        Integer id = enviorSupvsLogDao.saveEnviorSupvsLog(enviorSupvsLogEntity);
        if (id == null||id==0) {
            log.error("日志添加失败:{}", new Date());
            throw new RRException("日志添加失败");
        }
        return "日志添加成功,id:"+enid;
    }

    private EnviorSupvsLogEntity initEnviorSupvsLog(Long enid, String operation, Integer type) {

        EnviorSupvsLogEntity enviorSupvsLogEntity = new EnviorSupvsLogEntity();
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Object object = null;
        if (type == 1) {
            object = enviorSupvsService.queryInfo(enid.intValue());
        }else if (type==2){
            object = enviorSupvsService.queryPointList(new PointPageDto().setId(enid.intValue())).get(0);
        }
        enviorSupvsLogEntity.setJltime(new Date())
                .setEnid(enid)
                .setUserid(userEntity.getUserId())
                .setName(userEntity.getName())
                .setCzbz(operation)
                .setJson(JSON.toJSONString(object));
        return enviorSupvsLogEntity;
    }

}
